var appPath = getAppPath();

$(function(){
	initJspGrid();
});

// 样版列表初始化
function initJspGrid(){
	
	$('#coopAppGrid').datagrid({
	    url:appPath + "/itbCom/getCoopAppInfoPg.action",
	    height:'full',
	    //remoteSort:false,
	    striped:true,
	    fitColumns:true,
	    queryParams: getQueryParam(),
	    singleSelect:false,
	    // If set to true, clicking a checkbox will always select the row. 
	    // If false, selecting a row will not check the checkbox.
	    selectOnCheck:true,
	    // If true, the checkbox is checked/unchecked when the user clicks on a row. 
	    // If false, the checkbox is only checked/unchecked when the user clicks exactly on the checkbox.
	    checkOnSelect:true,
	    frozenColumns:[[
	        {field:'ck',checkbox:'true',width:10,align:'center',sortable:false}
	    ]],
	    columns:[[
  	        {field:'id',title:'申请ID',width:35,align:'center',sortable:false},
	        {field:'type',title:'类别',width:35,align:'center',sortable:false,
  	        	formatter:function(value, rowData, rowIndex){
	        		if(!rowData.id) return "N/A";
	        		var show = '<span style="color:red">未知</span>';
	        		switch(Number(value||'0')){
		        		case 10: show="普通申请"; break;
		        		case 20: show='<span style="color:green">城市合伙人</span>'; break;
		        	}
	        		return show;
	        	}
		    },
	        {field:'state',title:'状态',width:35,align:'center',sortable:false,
  	        	formatter:function(value, rowData, rowIndex){
	        		if(!rowData.id) return "N/A";
	        		var show = getState4Show(value);
	        		return "<a href='javascript:void(0)' onClick='editState("+rowData.id+")'>"+show+"</a>";
	        	}
		    },
	        {field:'organize',title:'机构名称',width:70,align:'center',sortable:false},
	        {field:'name',title:'申请者',width:60,align:'center',sortable:false},
	        {field:'mobile',title:'电话',width:50,align:'center',sortable:false},
	        {field:'email',title:'邮箱',width:65,align:'center',sortable:false},
	        {field:'qq',title:'QQ',width:50,align:'center',sortable:false},
	        {field:'address',title:'地址',width:50,align:'center',sortable:false,
	        	formatter:function(value, rowData, rowIndex){
	        		return (rowData.prov_name||'')+(rowData.city_name||'')+(rowData.dist_name||'');
	        	}
	        },
	        {field:'bank',title:'开户行',width:50,align:'center',sortable:false},
	        {field:'bank_account',title:'银行账户',width:50,align:'center',sortable:false},
	        {field:'user_id',title:'用户ID',width:35,align:'center',sortable:false},
	        {field:'user_mobile',title:'注册手机',width:50,align:'center',sortable:false},
	        {field:'create_time',title:'创建时间',width:45,align:'center',sortable:false},
	        {field:'update_time',title:'更新时间',width:45,align:'center',sortable:false}
	    ]],
	    hideColumn:['user_type'],
	    //view: imgView,// 自定义View
	    pagination:true,
		rownumbers:true,
		toolbar:[
			{
				text:'删除申请',
				iconCls:'icon-cancel',
				handler:function(){
					delCoopApp();
				}
			}
		]
	});
}

function getState4Show(value){
	var state = '<span style="color:red">未知</span>';
	switch(Number(value||'0')){
		case 10: state="新建"; break;
		case 20: state="审核中"; break;
		case 30: state="审核失败"; break;
		case 40: state="完成"; break;
	}
	return state;
}

// 状态编辑
function editState(id){
	$("#stateDlg").show();
	$('#stateForm').form('clear');

	$("#updAppId").val(id);
	$("#updAppDesc").val('');
	$("#curState").val('');
	$("#curType").val('');
	
	var url = appPath + "/itbCom/getCoopAppInfoLst.action?id="+id;
	$.get(url,function(ret){
		if (ret && ret.length>0){
			var info = ret[0];
			$("#updAppState").combobox('select',info.state||0);
			$("#updAppDesc").val(info.desc||'');
			$("#curState").val(info.state||0);
			$("#curType").val(info.type||0);
		}
	});
	
	$("#stateBtn").show();
	$('#stateDlg').window({
		title:'申请状态',
		iconCls:'icon-edit',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	
	$('#stateDlg').window('move',{top:20,left:20});
	$('#stateDlg').window('open');
}

function cancelUpdState(){
	$('#stateDlg').window('close');
}
function doUpdState(){
	var id = $("#updAppId").val();
	var desc = $("#updAppDesc").val();
	var curState = $("#curState").val();
	var curType = $("#curType").val();
	var updState = $("#updAppState").combobox('getValue');
	
	var url = appPath+"/itbComWeb/updCoopApp.action";
	var sendData = {
		"coopAppInfo.id": Number(id||"0"),
		"coopAppInfo.state": Number(updState||"0"),
		"coopAppInfo.desc": desc||''
	};
	
	if(updState==40) {// 通过申请
		$.messager.confirm("确认", "是否确认[通过]申请？",
			function(r){
		        if (r) { 
		        	$.loading(true);
		        	$.post(url, sendData, function(data){  
		    			if (data == "ok") { 
		    				
		    				if(curType==20) {// 城市合伙人申请
		    					var url2 = appPath+"/itbComWeb/addPartnerByApply.action"; 
			    				var sendData2 = {
		    						"apply_id":id
			    				};
			    				$.post(url2, sendData2, function(data2) {
			    					$.loading(false);
			    					if(data2=="ok") {
			    						//
			    						$.messager.alert('提示信息','提交成功','info');
					    				$('#stateDlg').window('close');
					    				doQuery();
			    					} else {
			    						$.messager.alert('提示信息','更新合伙人信息失败','info');
			    					}
			    				});
		    				} else {
		    					$.loading(false);
		    					$.messager.alert('提示信息','提交成功','info');
			    				$('#stateDlg').window('close');
			    				doQuery();
		    				}

		    	        } else {
		    	        	$.loading(false);
		    	        	$.messager.alert('提示信息','提交失败','info');
		    	        }
		    	    });
		        }
			}
		);
	
	} else {
		$.post(url, sendData, function(data){  
			if (data == "ok") { 
				$.messager.alert('提示信息','提交成功','info');
				$('#stateDlg').window('close');
				doQuery();
	        } else {
	        	$.messager.alert('提示信息','提交失败','info');
	        }
	    });
	}
}

function delCoopApp(){
	var selectItem=$("#coopAppGrid").datagrid("getChecked");
	if(!selectItem || selectItem.length == 0){
		$.messager.alert('提示信息','请选择申请项目！','warning');
		return;
	}
	for(var i=0; i<selectItem.length; i++){
		if(Number(selectItem[i].state||'0')==20){
			$.messager.alert('提示信息','不能删除[审核中]申请！','warning');
			return;
		}
	}
	
	$.messager.confirm("确认", "是否确认[删除]所选用户申请？",
		function(r){
	        if (r) { 
				var ids = "";
				$.each(selectItem, function(index, item){
					ids = ids + item.id + ",";
				});
				var url = appPath+"/itbComWeb/delCoopApp.action?ids="+ids;
				$.post(url,function(data){
					if(data=="ok"){
						$.messager.alert('提示信息','删除成功','info');
						doQuery();
					}else{
						$.messager.alert('提示信息','删除失败','info');
					}
				});
	        }
		}
	);
}

/**
 * 获取请求信息
 */
function getQueryParam(){
	var queryObject = {};
	
	var findType = $("#queryAppType").combobox('getValue');
	var findState = $("#queryAppState").combobox('getValue');
	var findMobile = $("#queryAppMobile").val();
	
	if(findType && Number(findType||'0')>0) queryObject.findAppType = findType;// 分类
	if(findState && Number(findState||'0')>0) queryObject.findAppState = findState;// 状态
	if(findMobile && Number(findMobile||'0')>0) queryObject.findAppMobile = findMobile;// 手机
	
	return queryObject;
}

/**
 * 查询数据
 */
function doQuery(){
	$("#coopAppGrid").datagrid('unselectAll');
	$("#coopAppGrid").datagrid('load', getQueryParam());
}

function fmtCreateTime(value, rowData, rowIndex){
	if(!value)return "";
	var s = new Date(value);
	return rowData.create_time=$.DateUtil.format(s,'yyyy/MM/dd hh:mm:ss');
}
